package com.lc.controller;

import com.lc.bean.Emp;
import com.lc.bean.Order;
import com.lc.service.OrderService;
import com.lc.utils.Constants;
import com.lc.utils.R;
import com.lc.vo.CgOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    // 查询所有订单列表
    @GetMapping("/list")
    public R list(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        return R.ok(orderService.list(pageNum,pageSize));
    }

    // 查询运输任务指派列表
    // 状态=2
    @GetMapping("/assignlist")
    public R assignlist() {
        return R.ok(orderService.assignlist());
    }

    @GetMapping("/transportlist")
    public R transportlist() {
        return R.ok(orderService.transportlist());
    }

    // 选中供应商，查询该供应商售卖的所有商品列表
    @GetMapping("/selectGoodsListBySupplierId/{id}")
    public R selectGoodsListBySupplierId(@PathVariable Integer id) {
        return R.ok(orderService.selectGoodsListBySupplierId(id));
    }

    // 保存采购订单
    @PostMapping("/cgSave")
    public R cgSave(@RequestBody CgOrderVo cgOrderVo, HttpSession session) { //requestbody 将json对象转为参数对象
        // CgOrderVo(supplieruuid=23, checker=7, items=[7-100, 8-200])
        Emp loginEmp = (Emp) session.getAttribute(Constants.LOGIN_USER_TAG);
        cgOrderVo.setLoginemp(loginEmp);
        orderService.saveCgOrder(cgOrderVo);
        return R.ok();
    }

    // 采购订单审批
    // 获取登录用户的id，查询checker=用户id的所有记录
    @GetMapping("/approveList")
    public R approveList(HttpSession session) {
        Emp loginEmp = (Emp) session.getAttribute(Constants.LOGIN_USER_TAG);
        List<Order> list = orderService.approveList(loginEmp.getUuid());

        return R.ok(list);
    }


    // 审批通过接口
    @GetMapping("/accept")
    public R accept(Integer orderuuid) {
        orderService.accept(orderuuid);
        return R.ok();
    }

    // 指派跟单员
    @GetMapping("/assign")
    public R assign(Integer orderuuid,Integer completer) {
        orderService.assign(orderuuid,completer);
        return R.ok();
    }
}
